package pyip.lib.javalearn.syntax;

import java.util.ArrayList;
import java.util.List;

public class GenericityMapBuilderDemo<T> {

    private List<T> list = new ArrayList<>();

    private GenericityMapBuilderDemo() {
    };

    public static <I> GenericityMapBuilderDemo<I> create() {
        return new GenericityMapBuilderDemo<I>();
    }

    public GenericityMapBuilderDemo<T> add(T value) {
        list.add(value);
        return this;
    }

    public List<T> build() {
        return list;
    }

    @SuppressWarnings("unused")
    public static void main(String[] args) {
        // 没有指定泛型, 也就是 T-->Object
        GenericityMapBuilderDemo.create().add("hah0").add(123).build();
        // 注意 .<String> create()
        List<String> list = GenericityMapBuilderDemo.<String> create().add("hah0").build();
        // 这里create()前可以不用带 <String>
        GenericityMapBuilderDemo<String> demo = GenericityMapBuilderDemo.create();
        demo.add("hah");
        List<String> list2 = demo.build();
    }
}